package com.baizhi.springmvc.service.imp;


import com.baizhi.springmvc.dao.UserDao;
import com.baizhi.springmvc.entity.User;
import com.baizhi.springmvc.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public User login(String name,String password) {
            User user = userDao.selectByName(name);
            if (user != null) {
                if (user.getUser_password().equals(password)) {
                    if (user.getUser_state() == 1) {
                        return user;
                    } else {
                        throw new RuntimeException("账号未激活!");
                    }
                } else {
                    throw new RuntimeException("密码错误!");
                }
            } else {
                throw new RuntimeException("账号或密码错误!");
            }
    }

    @Override
    public void register(User user) {
        userDao.insert(user);

    }

    @Override
    public void updateStatus(Integer id) {
        userDao.updateStatus(id);
    }

    @Override
    public User selectByName(String name) {
        User user = userDao.selectByName(name);
        return user;
    }
}
